﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserGroupDashboardController : DefaultController
	{
		public UserGroupDashboardController(IUserGroupService userGroupService)
		{
			this.UserGroupService = userGroupService;
		}

		public IUserGroupService UserGroupService
		{
			get; 
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Index(int? page)
		{
			var groups = UserGroupService.GetPaged(new UserGroupSpecification
			{
				UserId = Identity.Id,
				Page = page,
				Limit = Settings.Users.GroupPageLimit
			});

			var group = groups.FirstOrDefault();

			return group.CanView ? View(Views.Index, groups) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create()
		{
			var group = UserGroupService.Create();

			return group.CanCreate ? View(Views.Create, new UserGroupCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Create(UserGroupCreate value)
		{
			try
			{
				var group = UserGroupService.Create();

				if (!group.CanCreate)
				{
					return NotAuthorized();
				}

				UserGroupService.Insert(group, value);

				return Message(Routes.UserGroupDashboardUpdate, new { id = group.Id }, Resources.Messages.UserAlbumCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update(int id)
		{
			var group = UserGroupService.GetById(id);

			if (group == null)
			{
				return Error(Resources.Messages.UserGroupNotFound);
			}

			return group.CanUpdate || group.CanUpdateAny ? View(Views.Update, new UserGroupUpdate(group)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Update(UserGroupUpdate value)
		{
			try
			{
				var group = UserGroupService.GetById(value.Id);

				if (group == null)
				{
					return Error(Resources.Messages.UserGroupNotFound);
				}

				if (!group.CanUpdate && !group.CanUpdateAny)
				{
					return NotAuthorized();
				}

				UserGroupService.Update(group, value);

				return Message(Routes.UserGroupDashboardUpdate, Resources.Messages.UserGroupUpdated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Delete(int id)
		{
			var group = UserGroupService.GetById(id);

			if (group == null)
			{
				return Error(Resources.Messages.UserGroupNotFound);
			}

			return group.CanDelete || group.CanDeleteAny ? View(Views.Delete, new UserGroupDelete(group)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Delete(UserAlbumDelete value)
		{
			try
			{
				var group = UserGroupService.GetById(value.Id);

				if (group == null)
				{
					return Error(Resources.Messages.UserGroupNotFound);
				}

				if (!group.CanDelete && !group.CanDeleteAny)
				{
					return NotAuthorized();
				}

				UserGroupService.Delete(group);

				return RedirectToRoute(Routes.UserGroupDashboardIndex);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Delete, value);
		}
	}
}